<%--
  Created by IntelliJ IDEA.
  User: 兰宦君
  Date: 2015/10/2
  Time: 21:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
  <title>BookStore——我的网上书店</title>
  <%@ include file="../layout/import.jsp"%>
  <script language="JavaScript">
    function doPrice(type){

      if(type=="reduce"){
        var oldValue = $("#num").val();
        if(oldValue<=1){
          return;
        }else{
          oldValue--;
          $("#num").val(oldValue);
        }
      }

      if(type =="add"){
        var oldValue = $("#num").val();
        if(oldValue>=100){
          return;
        }else{
          oldValue++;
          $("#num").val(oldValue);
        }
      }
        updatePrice();
    }

    function updatePrice() {
        var num = $("#num").val();
        var totalprice = parseInt(num) * ${book.price};
        $("#totalprice")[0].innerHTML = totalprice.toFixed(2);
    }


    function addBuyCar(bookId){
      if(${isLogin == true}){
        $.ajax({
          url: "/addToCart.do",
          data: {id:bookId, num: $("#num").val()},
          method: "POST",
          dataType: "json",
          error: function () {
            alert("表单提交出错，请稍后再试");
          },
          success: function(response) {
            if(response.code == 0) {
              alert("已添加");
            }
            else {
              alert(response.description);
            }
          }
        });
      }else{
        alert("请你先登录或者注册，才能加入购物车");
      }
    }

    function buyNow(bookId){
        if(${isLogin == true}){
            $.ajax({
                url: "/addToCart.do",
                data: {id:bookId, num: $("#num").val()},
                method: "POST",
                dataType: "json",
                error: function () {
                    alert("表单提交出错，请稍后再试");
                },
                success: function(response) {
                    if(response.code == 0) {

                    }
                    else {
                        alert(response.description);
                    }
                }
            });
        }else{
            alert("请你先登录或者注册，才能加入购物车");
        }
    }
  </script>
</head>
<body>
<%@include file="nav.jsp"%>
  <div class="container" style="min-height: 75%">
    <h2>${book.bookname}</h2>
    <div class="col-sm-10">
        <div class="container">
          <div class="col-sm-4">
              <img style="width: 300px; height: 500px" src="${book.picture}" class="img-thumbnail">          </div>
            <div class="col-sm-8" style="font-size: 16px;">
                <br /><br /><br /><br />
                <p>ISBN：${book.isbn}</p>
                <p>作者：${book.author}</p>
                <p>简介：</p>
                <p>${book.content}</p>
                <p>价格：</p>
                <p>￥${book.price}</p>
            </div>
        </div>

        <div class="col-sm-4 col-sm-offset-6">
          <div class="input-append">
            您需要购买的数量是：
            <button class="btn btn-default" type="button" onclick="doPrice('reduce')">-</button>
            <input disabled="disabled" class="span2" type="tel" style="max-width: 50px" value="1" id="num">
            <button class="btn btn-default" type="button" onclick="doPrice('add')">+</button>
            <br />
            总价为：￥<span id="totalprice"></span>
            <script type="text/javascript">
                updatePrice();
            </script>
          </div>
        </div>
      <div class="col-sm-4 col-sm-offset-6">
          <br />
          <a class="btn btn-success" onclick="buyNow(${book.id})" href="/shoppingCart.do">立即购买</a>
        <button class="btn btn-danger col-sm-offset-1" onclick="addBuyCar(${book.id})">加入购物车</button>
      </div>
    </div>

    <div class="col-lg-2">
      <label>推荐书籍</label>
      <ul class="list-unstyled">
        <li>
          <img src="image/book.jpg" class="img-thumbnail">
          <input type="button" value="更多信息&raquo;">
        </li>

        <li>
          <img src="image/book.jpg" class="img-thumbnail">
          <input type="button" value="更多信息&raquo;">
        </li>

        <li>
          <img src="image/book.jpg" class="img-thumbnail">
          <input type="button" value="更多信息&raquo;">
        </li>
      </ul>
    </div>
  </div>
<%@include file="footer.jsp"%>
</body>
</html>
